from adlist.models import SouhuModel, ToutiaoModel, ToutiaoPatameter, SinaPatameter, SouhuPatameter, FenghuangPatameter, \
    RadarPatameter
from adlist.models import ToutiaoModel, RadarModel, SinaModel, SouhuModel, FenghuangModel
from ad_manage.models import Advertisement
from django.forms.models import model_to_dict

def advertisement_date_controller(advertisement, start, end):
    url = advertisement.url
    title = advertisement.title
    date_list = []
    day = ""
    hour = ""
    if advertisement.model_from == 'toutiao_app':
        toutiaoModel = ToutiaoModel.objects.filter(article_url=url)
        toutiao_ad_id = toutiaoModel[0].id
        datePatameter = ToutiaoPatameter.objects.filter(toutiao_ad_id=toutiao_ad_id).filter(time__range=(start, end))
        for to in datePatameter:

            if (str(to.time.year) + '-' + str(to.time.month) + '-' + str(to.time.day) == day and str(
                    to.time.hour) == hour):
                continue
            day = str(to.time.year) + '-' + str(to.time.month) + '-' + str(to.time.day)
            hour = str(to.time.hour)
            toutiao_date = {}
            toutiao_date['day'] = day
            toutiao_date['hour'] = hour
            date_list.append(toutiao_date)

    elif advertisement.model_from == 'sina_app':

        sinaModel = SinaModel.objects.filter(link=url)
        sina_id = sinaModel[0].id
        datePatameter = SinaPatameter.objects.filter(sina_ad_id=sina_id).filter(time__range=(start, end))
        for to in datePatameter:
            if (str(to.time.year) + '-' + str(to.time.month) + '-' + str(to.time.day) == day and str(
                    to.time.hour) == hour):
                continue
            day = str(to.time.year) + '-' + str(to.time.month) + '-' + str(to.time.day)
            hour = str(to.time.hour)
            sina_date = {}
            sina_date['day'] = day
            sina_date['hour'] = hour
            date_list.append(sina_date)

    elif advertisement.model_from == 'souhu_app':

        souhuModel = SouhuModel.objects.filter(link=url)
        souhu_id = souhuModel[0].id
        datePatameter = SouhuPatameter.objects.filter(souhu_ad_id=souhu_id).filter(time__range=(start, end))
        for to in datePatameter:
            if (str(to.time.year) + '-' + str(to.time.month) + '-' + str(to.time.day) == day and str(
                    to.time.hour) == hour):
                continue
            day = str(to.time.year) + '-' + str(to.time.month) + '-' + str(to.time.day)
            hour = str(to.time.hour)
            souhu_date = {}
            souhu_date['day'] = day
            souhu_date['hour'] = hour
            date_list.append(souhu_date)

    elif advertisement.model_from == 'fenghuang_app':

        fenghuangModel = FenghuangModel.objects.filter(title=title)
        fenghuang_id = fenghuangModel[0].id
        datePatameter = FenghuangPatameter.objects.filter(fenghuang_ad_id=fenghuang_id).filter(time__range=(start, end))
        for to in datePatameter:
            if (str(to.time.year) + '-' + str(to.time.month) + '-' + str(to.time.day) == day and str(
                    to.time.hour) == hour):
                continue
            day = str(to.time.year) + '-' + str(to.time.month) + '-' + str(to.time.day)
            hour = str(to.time.hour)
            fenghuang_date = {}
            fenghuang_date['day'] = day
            fenghuang_date['hour'] = hour
            date_list.append(fenghuang_date)

    elif advertisement.model_from == 'radar_app':
        radar = RadarModel.objects.filter(url=url)
        radar_id = RadarModel[0].id
        datePatameter = RadarPatameter.objects.filter(radar_ad_id=radar_id).filter(time__range=(start, end))
        for to in datePatameter:
            if (str(to.time.year) + '-' + str(to.time.month) + '-' + str(to.time.day) == day and str(
                    to.time.hour) == hour):
                continue
            day = str(to.time.year) + '-' + str(to.time.month) + '-' + str(to.time.day)
            hour = str(to.time.hour)
            radar_date = {}
            radar_date['day'] = day
            radar_date['hour'] = hour
            date_list.append(radar_date)


def tag_like_controller(advertisement):
    image_tag_list = []
    tag_list = [advertisement[0].image_tag_1, advertisement[0].image_tag_2, advertisement[0].image_tag_3, advertisement[0].image_tag_4, advertisement[0].image_tag_5,
                advertisement[0].image_tag_6, advertisement[0].image_tag_7, advertisement[0].image_tag_8, advertisement[0].image_tag_9, advertisement[0].image_tag_10]
    image_tag_list = image_tag_list + tag_list
    ad_dict = []
    remainder = 10
    for tag in image_tag_list:
        if len(ad_dict) < 10:
            advertisements = Advertisement.objects.filter(model_from='souhu_app').filter(image_tag_1=tag).exclude(
                image_tag_1='暂无')[0:remainder]
            ad_dict = ad_dict + [model_to_dict(item) for item in advertisements]
            if len(ad_dict) < 10:
                remainder = 10 - len(ad_dict)
        else:
            break
    return ad_dict